package com.wei.tang.ui;

import com.wei.tang.AppConest;
import com.wei.tang.AppManager;
import com.wei.tang.R;
import com.wei.tang.interf.BaseViewInterface;
import com.wei.tang.interf.OnTabReselectListener;
import com.wei.tang.util.Tlog;
import com.wei.tang.widget.MyFragmentTabHost;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;

@SuppressLint("InflateParams")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends FragmentActivity
		implements OnTabChangeListener, BaseViewInterface, View.OnClickListener, OnTouchListener {

	private MainActivity mySelf = this;

	@InjectView(android.R.id.tabhost)
	public MyFragmentTabHost mTabHost;

	private DoubleClickExitHelper mDoubleClickExit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ButterKnife.inject(this);
		initView();
		AppManager.getAppManager().addActivity(this);
	}

	@Override
	public void initView() {
		mDoubleClickExit = new DoubleClickExitHelper(this);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		if (android.os.Build.VERSION.SDK_INT > 10) {
			mTabHost.getTabWidget().setShowDividers(0);
		}

		initTabs();

		mTabHost.setCurrentTab(0);
		mTabHost.setOnTabChangedListener(this);
	}

	@Override
	public void initData() {
	}

	private void initTabs() {
		MainTab[] tabs = MainTab.values();
		final int size = tabs.length;
		for (int i = 0; i < size; i++) {
			MainTab mainTab = tabs[i];
			TabSpec tab = mTabHost.newTabSpec(getString(mainTab.getResName()));
			View indicator = LayoutInflater.from(getApplicationContext()).inflate(R.layout.tab_indicator, null);
			TextView title = (TextView) indicator.findViewById(R.id.tab_title);
			Drawable drawable = this.getResources().getDrawable(mainTab.getResIcon());
			title.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
			title.setText(getString(mainTab.getResName()));
			tab.setIndicator(indicator);
			tab.setContent(new TabContentFactory() {

				@Override
				public View createTabContent(String tag) {
					return new View(MainActivity.this);
				}
			});
			mTabHost.addTab(tab, mainTab.getClz(), null);
			mTabHost.getTabWidget().getChildAt(i).setOnTouchListener(this);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (AppConest.IS_RETURN_MAIN) {
			AppConest.IS_RETURN_MAIN = false;
		}
	}

	@Override
	public void onTabChanged(String tabId) {
		final int size = mTabHost.getTabWidget().getTabCount();
		for (int i = 0; i < size; i++) {
			View v = mTabHost.getTabWidget().getChildAt(i);
			if (i == mTabHost.getCurrentTab()) {
				v.setSelected(true);
			} else {
				v.setSelected(false);
			}
		}
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		super.onTouchEvent(event);
		boolean consumed = false;
		if (event.getAction() == MotionEvent.ACTION_DOWN && v.equals(mTabHost.getCurrentTabView())) {
			Fragment currentFragment = getCurrentFragment();
			if (currentFragment != null && currentFragment instanceof OnTabReselectListener) {
				OnTabReselectListener listener = (OnTabReselectListener) currentFragment;
				listener.onTabReselect();
				consumed = true;
			}
		}
		return consumed;
	}

	private Fragment getCurrentFragment() {
		return getFragmentManager().findFragmentByTag(mTabHost.getCurrentTabTag());
	}

	/**
	 * 监听返回--是否退出程序
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return mDoubleClickExit.onKeyDown(keyCode, event);
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
	}

	@Override
	public void onClick(View v) {
	}

	@Override
	public int getLayoutId() {
		return 0;
	}
}
